/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.query;

import org.ipig.commons.helper.GsonHelper;
import org.ipig.commons.helper.HttpHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpPost;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * QueryClient
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: QueryClient.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@Slf4j
public class QueryClient {
    /**
     * query
     *
     * @param url
     * @param so
     * @return QueryResult
     */
    public static QueryResult query(String url, QuerySo so) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        QuerySo[] soArgs = Arrays.asList(so).stream().toArray(QuerySo[]::new);
        Map<QuerySo, QueryResult> resultMap = query(url, soArgs);
        return resultMap.get(so);
    }

    /**
     * query
     *
     * @param url
     * @param soArgs
     * @return Map<QuerySo, QueryResult>
     */
    public static Map<QuerySo, QueryResult> query(String url, QuerySo[] soArgs) {
        Map<QuerySo, QueryResult> resultMap = null;
        if (soArgs == null || soArgs.length == 0) {
            resultMap = new HashMap<>(0);
        } else {
            String parms = GsonHelper.toJson(soArgs);
            log.debug("查询参数：{}", parms);
            HttpPost httpPost = HttpHelper.getHttpPostToJson(url, parms);
            String msg = HttpHelper.doHttpPost(HttpHelper.getCloseableHttpClient(), httpPost);
            httpPost.releaseConnection();
            log.debug("查询结果：{}", msg);
            int length = soArgs.length;
            resultMap = new HashMap<>(length);
            for (int i = 0; i < length; i++) {
                QuerySo so = soArgs[i];
                resultMap.put(so, new QueryResult(so, msg));
            }
        }
        return resultMap;
    }

    public static void main(String[] args) {
        String url = "http://192.168.8.16:18080/data-remoting/query/stat";
//        String url = "http://hrb2:18080/data-remoting/query/stat";
//        String url = "http://127.0.0.1:18080/data-remoting/query/stat";
        try {
            QuerySo so = new QuerySo();
            so.setBiz("JKF");
            so.setDim("JKFIM001");
            so.setKey("2992_502_I");
            QueryVar var = new QueryVar();
            var.setName("total");
            var.setRef("20000101010111");
            var.setDur("100ya");
            so.getVars().add(var);

            QuerySo[] soArgs = Arrays.asList(so).stream().toArray(QuerySo[]::new);
            Map<QuerySo, QueryResult> resultMap = QueryClient.query(url, soArgs);
            System.out.println(resultMap.get(so).getResult(var));

            QueryResult result = QueryClient.query(url, so);
            System.out.println(result.getResult(var));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
